<?php
/**
 * Hesperos libraries
 *
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2011 Krzysztof Kardasz
 * @category     Hesperos
 * @package      Model
 * @subpackage   Domain
 * @version 	 SVN: $Id$
 * @filesource
 */

/**
 * Domain Object
 *
 * @author       Krzysztof Kardasz <krzysztof.kardasz@gmail.com>
 * @copyright    Copyright (c) 2011 Krzysztof Kardasz
 * @category     Hesperos
 * @package      Model
 * @subpackage   Domain
 */
abstract class Hesperos_Model_Domain_Object
{

    /**
     * Konstruktor
     *
     * @param $options
     * @return void
     */
    public function __construct (array $options = null)
    {
        if ($options !== null) {
            $this->_setOptions($options);
        }
    }

    /**
     * Definiowanie wartości
     * Metoda zamienia wywołanie $this->pole = wartosc na wywołanie $this->setPole(wartosc)
     *
     * @param  string $name nazwa
     * @param  mixed $value wartość
     * @return void
     * @throws Exception
     */
    public function __set ($name, $value)
    {
        $method = 'set' . ucfirst($name);
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property: ' . $name);
        }
        $this->$method($value);
    }

    /**
     * Zwracanie wartości
     * Metoda zamienia wywołanie $this->pole na wywołanie $this->getPole()
     *
     * @param  string $name nazwa
     * @return mixed
     * @throw Exception
     */
    public function __get ($name)
    {
        $method = 'get' . ucfirst($name);
        if (('mapper' == $name) || !method_exists($this, $method)) {
            throw new Exception('Invalid property: ' . $name);
        }
        return $this->$method();
    }

    /**
     * Definiowanie wartości
     *
     * @param  array $options dane
     * @return Object
     */
    protected function _setOptions (array $options)
    {
        $methods = get_class_methods($this);
        foreach ($options as $key => $value) {
            $method = 'set' . ucfirst($key);
            if (in_array($method, $methods)) {
                $this->$method($value);
            }
        }
        return $this;
    }
}